package com.sscaipiao.android.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sscaipiao.android.R;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.BaseHelper;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.StringUtil;
import com.sscaipiao.android.utils.UserDataServiceHelper;

public class StartTogetherActivity extends Activity implements OnClickListener {
	
	
	private User user;
	private String date;
	private String schemeNum1;
	private int totalMoney;
	private AlertDialog adc;
	private Button back;
	
	private TextView hemai_tv_total_money;
	private TextView hemai_tv_total_amount;
	private EditText hemai_edit_text_meifenjine;
	private EditText hemai_editText_yongjin;
	private EditText hemai_edit_text_woyaorengou;
	private EditText hemai_editText_woyaobaodi;
	private EditText hemai_edit_txt_announce;
	private TextView hemai_tv_botton_var;
	private TextView hemai_begin_tv_share;
	private TextView hemai_bt_submit;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.begin_hemai);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		Intent i = this.getIntent();
		if (!i.equals(null)) {
			Bundle b = i.getExtras();
			if (b != null) {
				if (b.containsKey(Constants.USER)) {
					user = (User) b.get(Constants.USER);
				}
				if (b.containsKey("date")) {
					//期数
					date = (String) b.get("date");
				}
				if (b.containsKey("schemeNum1")) {
					//投注格式
					schemeNum1 = (String) b.get("schemeNum1");
				}
				if (b.containsKey("totalMoney")) {
					//投注格式
					totalMoney = (Integer) b.get("totalMoney");
				}

			}
		}
		
		back = (Button)findViewById(R.id.hemai_begin_back);
		
		hemai_tv_total_money = (TextView)findViewById(R.id.hemai_tv_total_money);
		hemai_tv_total_amount = (TextView)findViewById(R.id.hemai_tv_total_amount);
		hemai_edit_text_meifenjine = (EditText)findViewById(R.id.hemai_edit_text_meifenjine);
		hemai_editText_yongjin = (EditText)findViewById(R.id.hemai_editText_yongjin);
		hemai_edit_text_woyaorengou = (EditText)findViewById(R.id.hemai_edit_text_woyaorengou);
		hemai_editText_woyaobaodi = (EditText)findViewById(R.id.hemai_editText_woyaobaodi);
		hemai_edit_txt_announce = (EditText)findViewById(R.id.hemai_edit_txt_announce);
		hemai_tv_botton_var = (TextView)findViewById(R.id.hemai_tv_botton_var);
		hemai_begin_tv_share = (TextView)findViewById(R.id.hemai_begin_tv_share);
		hemai_bt_submit = (TextView)findViewById(R.id.hemai_bt_submit);
		
		String sAgeFormat1 = getResources().getString(R.string.hemai_total_money); 
		hemai_tv_total_money.setText((String.format(sAgeFormat1,totalMoney+"")));
		
		resetTotalFenshu();
		
		resetBottomBar();
		
		hemai_edit_text_meifenjine.addTextChangedListener(new TextWatcher() {  
            @Override  
            public void onTextChanged(CharSequence s, int start, int before, int count) {  
                // TODO Auto-generated method stub  
            }  
  
            @Override  
            public void beforeTextChanged(CharSequence s, int start, int count,  
                    int after) {  
                // TODO Auto-generated method stub  
                  
            }  
              
            @Override  
            public void afterTextChanged(Editable s) {  

        		if(StringUtil.isNotEmptyString(hemai_edit_text_meifenjine.getText().toString()) &&
        				Integer.parseInt(hemai_edit_text_meifenjine.getText().toString()) >=1 &&
        				Integer.parseInt(hemai_edit_text_meifenjine.getText().toString()) <=totalMoney){
            		String sAgeFormat1 = getResources().getString(R.string.hemai_divid_into); 
//        			hemai_tv_total_amount.setText(String.format(sAgeFormat1, 
//        					(totalMoney / Integer.parseInt(hemai_edit_text_meifenjine.getText().toString())) + ""));
            		resetTotalFenshu();
            		resetBottomBar();
        		}

            }  
        }); 
		hemai_edit_text_woyaorengou.addTextChangedListener(new TextWatcher() {  
            @Override  
            public void onTextChanged(CharSequence s, int start, int before, int count) {  
                // TODO Auto-generated method stub  
            }  
  
            @Override  
            public void beforeTextChanged(CharSequence s, int start, int count,  
                    int after) {  
                // TODO Auto-generated method stub  
                  
            }  
              
            @Override  
            public void afterTextChanged(Editable s) {  

        		if(StringUtil.isNotEmptyString(hemai_edit_text_meifenjine.getText().toString()) &&
        				Integer.parseInt(hemai_edit_text_meifenjine.getText().toString()) >=1 &&
        				Integer.parseInt(hemai_edit_text_meifenjine.getText().toString()) <=totalMoney){
            		String sAgeFormat1 = getResources().getString(R.string.hemai_divid_into); 
//        			hemai_tv_total_amount.setText(String.format(sAgeFormat1, 
//        					(totalMoney / Integer.parseInt(hemai_edit_text_meifenjine.getText().toString())) + ""));
            		resetTotalFenshu();
            		resetBottomBar();
        		}

            }  
        }); 
		hemai_editText_woyaobaodi.addTextChangedListener(new TextWatcher() {  
            @Override  
            public void onTextChanged(CharSequence s, int start, int before, int count) {  
                // TODO Auto-generated method stub  
            }  
  
            @Override  
            public void beforeTextChanged(CharSequence s, int start, int count,  
                    int after) {  
                // TODO Auto-generated method stub  
                  
            }  
              
            @Override  
            public void afterTextChanged(Editable s) {  

        		if(StringUtil.isNotEmptyString(hemai_edit_text_meifenjine.getText().toString()) &&
        				Integer.parseInt(hemai_edit_text_meifenjine.getText().toString()) >=1 &&
        				Integer.parseInt(hemai_edit_text_meifenjine.getText().toString()) <=totalMoney){
            		String sAgeFormat1 = getResources().getString(R.string.hemai_divid_into); 
//        			hemai_tv_total_amount.setText(String.format(sAgeFormat1, 
//        					(totalMoney / Integer.parseInt(hemai_edit_text_meifenjine.getText().toString())) + ""));
            		resetTotalFenshu();
            		resetBottomBar();
        		}

            }  
        }); 
		back.setOnClickListener(this);
		hemai_bt_submit.setOnClickListener(this);
		
	}

	/**
	 * 
	 */
	private void resetBottomBar() {
		String sAgeFormat1;
		sAgeFormat1 = getResources().getString(R.string.hemai_botton_line); 
		String meifenjine = hemai_edit_text_meifenjine.getText().toString();
		String woyaobaodi = hemai_editText_woyaobaodi.getText().toString();
		String woyaorengou = hemai_edit_text_woyaorengou.getText().toString();
		hemai_tv_botton_var.setText(String.format(sAgeFormat1, 
				StringUtil.isEmptyString(woyaorengou) ? "0" : woyaorengou,
				Integer.parseInt( StringUtil.isEmptyString(meifenjine) ? "0" : meifenjine) * Integer.parseInt(StringUtil.isEmptyString(woyaorengou) ? "0" : woyaorengou) + "",
				StringUtil.isEmptyString(woyaobaodi) ? "0" : woyaobaodi,
				Integer.parseInt( StringUtil.isEmptyString(meifenjine) ? "0" : meifenjine) * Integer.parseInt(StringUtil.isEmptyString(woyaobaodi) ? "0" : woyaobaodi) + "",
				Integer.parseInt( StringUtil.isEmptyString(meifenjine) ? "0" : meifenjine) * Integer.parseInt(StringUtil.isEmptyString(woyaorengou) ? "0" : woyaorengou) + "",
				Integer.parseInt( StringUtil.isEmptyString(meifenjine) ? "0" : meifenjine) * Integer.parseInt(StringUtil.isEmptyString(woyaobaodi) ? "0" : woyaobaodi) + "",
				Integer.parseInt( StringUtil.isEmptyString(meifenjine) ? "0" : meifenjine) * Integer.parseInt(StringUtil.isEmptyString(woyaorengou) ? "0" : woyaorengou) + 
				Integer.parseInt( StringUtil.isEmptyString(meifenjine) ? "0" : meifenjine) * Integer.parseInt(StringUtil.isEmptyString(woyaobaodi) ? "0" : woyaobaodi)+ ""));
	}

	/**
	 * 
	 */
	private void resetTotalFenshu() {
		String sAgeFormat1;
		String meifenjine = hemai_edit_text_meifenjine.getText().toString();
		sAgeFormat1 = getResources().getString(R.string.hemai_divid_into); 
		hemai_tv_total_amount.setText(String.format(sAgeFormat1, 
				(totalMoney / Integer.parseInt( StringUtil.isEmptyString(meifenjine) ? "0" : meifenjine)) + ""));
	}

	@Override
	public void onClick(View v) {
		if(R.id.hemai_begin_back  == v.getId()) {

			StartTogetherActivity.this.finish();
			
		} else if ( R.id.hemai_bt_submit == v.getId()) {
			if( StringUtil.isEmptyString(hemai_edit_text_meifenjine.getText().toString()) || 
					totalMoney % Integer.parseInt(hemai_edit_text_meifenjine.getText().toString()) != 0) {
				Toast.makeText(StartTogetherActivity.this, "总额无法整除每份金额", Toast.LENGTH_SHORT).show();
				return;
			} else if(StringUtil.isEmptyString(hemai_editText_yongjin.getText().toString()) ||  
					Integer.parseInt(hemai_editText_yongjin.getText().toString()) < 0 ||
					Integer.parseInt(hemai_editText_yongjin.getText().toString()) > 100) {
				Toast.makeText(StartTogetherActivity.this, "请输入合法佣金比例", Toast.LENGTH_SHORT).show();
				return;
			}else if(StringUtil.isEmptyString(hemai_edit_text_woyaorengou.getText().toString()) ||  
					Integer.parseInt(hemai_edit_text_woyaorengou.getText().toString()) < 0 ||
					Integer.parseInt(hemai_edit_text_woyaorengou.getText().toString()) >
						(totalMoney / Integer.parseInt(hemai_edit_text_meifenjine.getText().toString())) - Integer.parseInt(hemai_editText_woyaobaodi.getText().toString())) {
				Toast.makeText(StartTogetherActivity.this, "请输入认购数量,认购+保底不得超过总数", Toast.LENGTH_SHORT).show();
				return;
			}else if(StringUtil.isEmptyString(hemai_editText_woyaobaodi.getText().toString()) ||  
					Integer.parseInt(hemai_editText_woyaobaodi.getText().toString()) < 0 ||
					Integer.parseInt(hemai_editText_woyaobaodi.getText().toString()) > 
						(totalMoney / Integer.parseInt(hemai_edit_text_meifenjine.getText().toString())) -Integer.parseInt(hemai_edit_text_woyaorengou.getText().toString())  ) {
				Toast.makeText(StartTogetherActivity.this, "请输入认购数量,认购+保底不得超过总数", Toast.LENGTH_SHORT).show();
				return;
			}else if(StringUtil.isEmptyString(hemai_edit_txt_announce.getText().toString())   ) {
				Toast.makeText(StartTogetherActivity.this, "合买宣言不能为空", Toast.LENGTH_SHORT).show();
				return;
			} else {

					AlertDialog.Builder ad = new AlertDialog.Builder(this);
					ad.setTitle("您已成功发起合买");
					ad.setPositiveButton("确认", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							
							String retStr = CaipiaoServiceHelper.betHMSSQ(StartTogetherActivity.this, 
									user, schemeNum1, date,
									hemai_edit_txt_announce.getText().toString(),
									hemai_editText_yongjin.getText().toString(),
									totalMoney % Integer.parseInt(hemai_edit_text_meifenjine.getText().toString())  + "",
									hemai_edit_text_woyaorengou.getText().toString(),
									null,
									hemai_editText_woyaobaodi.getText().toString());
							if(retStr.startsWith(Constants.LOGIN_SUCCESS_CODE)){
								adc.dismiss();
								StartTogetherActivity.this.finish();
							}else {
								adc.dismiss();
							}
						}
					});
					ad.setNegativeButton("取消", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							adc.dismiss();
						}

					});

					adc = ad.create();
					adc.show();
					


		}
	}
  }

}
